window.PARK = {
    showMask: function () {
        $('.park-mask').show();
    },
    hideMask: function () {
        $('.park-mask').hide();
    },
    toggleMask: function () {
        $('.park-mask').toggle();
    },
    // 上拉事件
    dropLoad: function (callback) {
        function scrollFn () {
            // 页面高度
            var pageHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            // 视窗高度
            var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
            // 隐藏高度
            var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

            if (pageHeight - viewportHeight - scrollHeight < 20) { //如果满足条件触发
                callback();
            }
        }

        $(window).on('scroll', scrollFn);
        callback();
    },
    // 取消上拉事件
    unDropLoad: function () {
        $(window).unbind('scroll');
    },
    // 分页加载
    dropAjax: function (options, url, params,  callback) {
        var defaults = {
            type: 'GET',
            dataType: 'json'
        }

        var settings = $.extend(defaults, options);

        var isSend = false;
        var p = 1;
        var params = params;
        return function () {
            if (!isSend) {
                params.p = p; // 分页字段
                $.ajax({
                    type: settings.type,
                    url: url,
                    dataType: settings.dataType,
                    data: params,
                    beforeSend: function () {
                        isSend = true;
                    },
                    complete: function () {
                        isSend = false;
                    },
                    success: function (data) {
                        p ++;
                        callback(data);
                    }
                });
            }
        }
    },
    // 转换base64
    getBase64: function(file, callback) {
        var file_img = file;
        if (!/^(image)\/(png|jpeg|jpg|gif)$/.test(file_img.type)) {
            alert('请选择一张图片');
            return false;
        }
        var FReader = new FileReader();

        FReader.readAsDataURL(file_img);

        FReader.onload = function (FREvent) {
            callback(FREvent.target.result);
        }
    },
    // 上传文件
    ajaxFileData: function (url, type, file, callback) {
        var formData = new FormData();
        /*for (var key in data) {
            if (data.hasOwnProperty(key)) {
                formData.append('file[]', data[key]);
            }
        }*/
        formData.append('file', file);
        $.ajax({
            type: type,
            url: url,
            data: formData,
            processData: false,
            contentType: false,
            success: function (res) {
                callback(res);
            }
        });
    },
    init: function () {
        var _this = this;
        // 绑定.park-back 点击返回
        $(document).on('click', '.park-back', function () {
            history.back();
        });
        // 点击遮罩关闭遮罩
        $(document).on('click', '.park-mask', function () {
            _this.hideMask();
        });
    }
};

PARK.init();